package com.ocean.everyday.d003_strongpasswordcheckerii;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode.cn/problems/strong-password-checker-ii/
 */
public class StrongPasswordCheckerii {
    public static void main(String[] args) {
        StrongPasswordCheckerii checker = new StrongPasswordCheckerii();
        System.out.println(checker.strongPasswordCheckerII("IloveLe3tcode!"));
        System.out.println(checker.strongPasswordCheckerII("Me+You--IsMyDream"));
        System.out.println(checker.strongPasswordCheckerII("1aB!"));
    }

    public boolean strongPasswordCheckerII(String password) {
        if (password.length() < 8) {
            return false;
        }

        Set<Character> specials = new HashSet<Character>() {{
            add('!');
            add('@');
            add('#');
            add('$');
            add('%');
            add('^');
            add('&');
            add('*');
            add('(');
            add(')');
            add('-');
            add('+');
        }};
        int n = password.length();
        boolean hasSpecials = false;
        boolean hasLower = false;
        boolean hashUpper = false;
        boolean hasDigit = false;
        for (int i = 0; i < n; i++) {
            char cur = password.charAt(i);
            if (i != 0 && cur == password.charAt(i - 1)) {
                return false;
            }
            if (specials.contains(cur)) {
                hasSpecials = true;
            }
            if (Character.isDigit(cur)) {
                hasDigit = true;
            }
            if (Character.isLowerCase(cur)) {
                hasLower = true;
            }
            if (Character.isUpperCase(cur)) {
                hashUpper = true;
            }
        }
        return hasSpecials && hasLower && hashUpper && hasDigit;
    }
}
